<template>
  <j-modal
    :title="params.title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <j-form-container>
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-form-model-item :label="params.label" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-select
              ref="select"
              v-model="model[params.modelCode]"
              allowClear
              show-search
              mode="multiple"
              :filter-option="filterOption"
              :placeholder="`请选择${params.label}`"
            >
              <a-select-option v-for="item in list" :key="item[params.idCode]" :value="item[params.idCode]">{{
                item[params.nameCode]
              }}</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-form-model>
      </j-form-container>
    </a-spin>
  </j-modal>
</template>

<script>
import { getAction } from '@/api/manage.js'
export default {
  name: 'SkClassModal',

  data() {
    return {
      width: 800,
      visible: false,
      disableSubmit: false,
      confirmLoading: false,
      model: {
        // teacherIds:undefined
      },
      validatorRules: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      list: [],
      params: {}
    }
  },
  created() {},
  methods: {
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    getList(url,id) {
      const params = {
        pageNo: 1,
        pageSize: 1000,
        areaId:id
      }
      getAction(url, params)
        .then(res => {
          if (res.success) {
            this.list = res.result.records
          } else {
            this.$message.warning(res.message)
          }
        })
        .catch(error => {
          console.error(error)
        })
    },
    getInfo(url, id) {
      let data = {
        areaId: id
      }
      getAction(url, data)
        .then(res => {
          this.model[this.params.modelCode] = res.result
        })
        .catch(error => {
          console.error(error)
        })
    },
    async init(json) {
      this.visible = true
      this.params = json
      this.$set(this.model, json.modelCode, [])
      await this.getList(json.list, json.areaId)
      await this.getInfo(json.info, json.areaId)
    },
    handleOk() {
      let data = {
        areaId: this.params.areaId
      }
      data[this.params.modelCode] = this.model[this.params.modelCode].join(',') 
      getAction(this.params.submit, data)
        .then(res => {
          this.$message.success("操作成功！")
          this.handleCancel()
        })
        .catch(error => {
          this.$message.warning(error.message)
        })
    },

    handleCancel() {
      this.visible = false
    }
  }
}
</script>
